<template>
	<u-popup :show="visible" mode="center" round="16rpx" :safeAreaInsetBottom="false" @close="hide">
		<view class="com-update-chatname">
			<view class="com-update-chatname__title">修改名称</view>
			<view class="com-update-chatname__content">
				<u-input v-model="roomName" placeholder="请输入" border="none"></u-input>
			</view>
			<view class="com-update-chatname__btns">
				<view class="com-update-chatname__btn bg-hover" @tap="hide">
					<text>取消</text>
				</view>
				<view class="com-update-chatname__btn bg-hover" @tap="confirmHandle">
					<u-loading-icon v-if="loading"></u-loading-icon>
					<text>确认</text>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				openId: '',
				roomId: '',
				roomName: '',
				loading: false,
			}
		},
		methods: {
			show(data) {
				console.log(data);
				this.visible = !this.visible
				this.openId = data.openId
				this.roomId = data.roomId
				this.roomName = data.roomName
			},
			hide() {
				this.visible = false
			},
			async confirmHandle() {
				let params = {
					openId: this.openId,
					roomId: this.roomId,
					roomName: this.roomName
				}
				this.loading = true
				let res = await this.$api.updateRoomName(params)
				if (res.success) {
					uni.showToast({ title: '修改成功！', icon: 'none' })
					this.loading = false
					
					this.hide()
					this.$emit('refresh', this.roomName)
				} else {
					uni.showToast({ title: res.message || '修改失败！', icon: 'none' })
					this.loading = false
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.com-update-chatname {
		width: 622rpx;
		padding-top: 40rpx;

		.com-update-chatname__title {
			padding: 0 32rpx;
			font-size: 34rpx;
			font-weight: 500;
			color: #1D2129;
			line-height: 48rpx;
			text-align: center;
		}

		.com-update-chatname__content {
			margin: 24rpx 32rpx 0;
			padding: 24rpx 20rpx;
			font-size: 32rpx;
			color: #1D2129;
			line-height: 44rpx;
			background-color: #F7F8FA;
		}

		.com-update-chatname__btns {
			margin-top: 40rpx;
			border-top: 2rpx solid #E5E6EB;

			display: flex;
			align-items: center;

			.com-update-chatname__btn {
				width: 100%;
				padding: 22rpx 32rpx;
				font-size: 32rpx;
				color: #276AEE;
				line-height: 44rpx;

				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
</style>